package com.itheima.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Map;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import com.itheima.bean.User;
import com.itheima.service.LoginService;

public class LoginFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException {
//		HttpServletRequest request = (HttpServletRequest)req;
//		chain.doFilter(new myCode(request), res);
		
		//把servletReuqest强转为HttpServletRequest
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)res;
		//判断session中是否有用户存在
		String username1=(String)request.getSession().getAttribute("username");
		if(username1!=null){
			//放行
//			chain.doFilter(request, response);
			chain.doFilter(new myCodeParameter(request), res);
			return;
		}
		//获取所有的cookie
		Cookie[] cookies = request.getCookies();
		Cookie co = null;
		//判断cookies是否为null
		if(cookies!=null){
			//从cookies中查找与usernameAndPwd相匹配的cookie
			for(Cookie cookie:cookies){
				if("usernameAndPwd".equals(cookie.getName())){
					co=cookie;
				}
			}
		}
		
		//判断是否是第一次登录
		if(co!=null){
			
			//获取cookie中的用户名和密码
			String up = co.getValue();
			System.out.println(co.getValue());
			String[] sp = up.split("-");
			String username = sp[0];
			String password = sp[1];
			//完成自动登录,调用LoginService
			LoginService ls = new LoginService();
			try {
				User user = ls.getUserByUsernameAndPwd(username, password);
				System.out.println("查询一次数据库");
				if(user!=null){
					//保证用户在线
					request.getSession().setAttribute("username", user.getUsername());
				}
				//放行
//				chain.doFilter(request, response);
				chain.doFilter(new myCodeParameter(request), res);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}else{
//			chain.doFilter(request, response);
			chain.doFilter(new myCodeParameter(request), res);
		}
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

}

class myCodeParameter extends HttpServletRequestWrapper {

	public myCodeParameter(HttpServletRequest request) {
		super(request);
		// TODO Auto-generated constructor stub
	}

	@Override
	public String getParameter(String name) {
		String method = super.getMethod();
		if("get".equalsIgnoreCase(method)){
//			String value = super.getParameter("username");
//			try {
//				value = new String(value.getBytes("iso-8859-1"),"utf-8");
//			} catch (UnsupportedEncodingException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			return value;
			//返回值有问题,全部拦截了
//			username=阿根
//			password=阿根
//			yzm=阿根
//			yzm=阿根
			//编码转换
			String value=super.getParameter(name);
			System.out.println("value="+value);
			//判断是参数提交才转换编码
			if (null!=value) {
				try {
					value = new String(value.getBytes("iso-8859-1"),"utf-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			return value;
		}else if("post".equalsIgnoreCase(method)){
			try {
				super.setCharacterEncoding("utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return super.getParameter(name);
		}
		return super.getParameter(name);
	}

}
